package com.lzq.chatroom_project.service.impl;

import com.lzq.chatroom_project.common.utils.CacheUtil;
import com.lzq.chatroom_project.common.utils.JacksonUtil;
import com.lzq.chatroom_project.dao.MessageMapper;
import com.lzq.chatroom_project.dao.MessageSessionMapper;
import com.lzq.chatroom_project.model.Message;
import com.lzq.chatroom_project.model.User;
import com.lzq.chatroom_project.service.BitmapService;
import com.lzq.chatroom_project.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.lzq.chatroom_project.common.constants.CacheKeyConstants.*;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private MessageSessionMapper messageSessionMapper;
    @Autowired
    private BitmapService bitmapService;
    @Autowired
    private CacheUtil cacheUtil;

    @Override
    public List<Message> getMessageList(Integer sessionId,Integer userId) {
        return messageMapper.getMessageListBySessionId(sessionId, userId);
    }

    @Override
    public List<Message> getHistoryMessageByDate(Integer sessionId, LocalDate date) {
        if (!checkMessagesByDate(sessionId, date)) {
            return new ArrayList<>();
        }
        if (cacheUtil.hasKey(DATE_MESSAGES_KEY +sessionId+"_"+date)) {
            List<Message> cachedMessages = JacksonUtil.readListValue(cacheUtil.get(DATE_MESSAGES_KEY +sessionId+"_"+date), Message.class);
            return cachedMessages.stream()
                    .filter(m -> m.getPostTime().toLocalDate().equals(date))
                    .collect(Collectors.toList());
        }
        List<Message> messages = messageMapper.findBySessionIdAndDate(sessionId, date);
        cacheUtil.set(DATE_MESSAGES_KEY +sessionId+"_"+date, JacksonUtil.writeValueAsString(messages));
        return messages;
    }

    @Override
    public List<Message> getHistoryMessageByKeyword(Integer sessionId, String keyword) {
        if (cacheUtil.hasKey(KEYWORD_MESSAGES_KEY +sessionId+"_"+keyword)) {
            List<Message> cachedMessages = JacksonUtil.readListValue(cacheUtil.get(KEYWORD_MESSAGES_KEY +sessionId+"_"+keyword), Message.class);
            return cachedMessages;
        }
        List<Message> messages = messageMapper.searchMessages(sessionId, keyword);
        cacheUtil.set(KEYWORD_MESSAGES_KEY +sessionId+"_"+keyword, JacksonUtil.writeValueAsString(messages));
        return messages;
    }

    public Boolean checkMessagesByDate(Integer sessionId, LocalDate date) {
        Boolean hasMessage = false;
        List<Integer> userIdsList;
        if (cacheUtil.hasKey(SESSION_USER_KEY + sessionId)) {
             userIdsList = JacksonUtil.readListValue(cacheUtil.get(SESSION_USER_KEY + sessionId), Integer.class);
        } else {
             userIdsList = messageSessionMapper.queryUserIdsBySessionId(sessionId);
            cacheUtil.set(SESSION_USER_KEY + sessionId, JacksonUtil.writeValueAsString(userIdsList));
        }
        for (Integer userId : userIdsList) {
            if (bitmapService.hasMessagesOnDate(sessionId, userId, date)) {
                hasMessage = true;
                break;
            }
        }
        return hasMessage;
    }
}
